package com.aaa.dao.impl;

import com.aaa.dao.RecharGeruleDao;
import com.aaa.entity.RecharGerule;
import com.aaa.util.BaseDao;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;

public class RecharGeruleDaoImpl implements RecharGeruleDao {

    private BaseDao baseDao = BaseDao.getInstance();

    @Override
    public int addRecharGerule(RecharGerule recharGerule) {
        String sql = "insert into rechargerule(name,coefficient" +
                ",createdTime,endTime,status,startMoney)values(?,?,?,?,?,?)";
        Object[] params = {recharGerule.getName(),recharGerule.getCoefficient(),
                recharGerule.getCreatedTime(),recharGerule.getEndTime(),
                recharGerule.getStatus(),recharGerule.getStartMoney()
        };
        return baseDao.executeUpdate(sql,params);
    }

    @Override
    public int updateRecharGeruleById(RecharGerule recharGerule) {
        String sql = "update rechargerule set name = ?,coefficient = ?,createdTime = ?,endTime = ?," +
                "status = ?,startMoney = ? where id = ?";
        Object[] params = {recharGerule.getName(),recharGerule.getCoefficient(),
                recharGerule.getCreatedTime(),recharGerule.getEndTime(),
                recharGerule.getStatus(),recharGerule.getStartMoney(),recharGerule.getId()
        };

        return baseDao.executeUpdate(sql,params);
    }

    @Override
    public int updateRecharGeruleStatusById(Integer id, Integer status) {
        String sql = "update rechargerule set status = ? where id = ? ";
        Object[] params = {status,id};
        return baseDao.executeUpdate(sql,params);
    }

    @Override
    public List<RecharGerule> findAllAndSearchRecharGerule(Integer pageNum, Integer pageSize, String name, String status) {
        String sql = "select * from rechargerule where 1 = 1 ";
        if (StringUtils.isNotBlank(name)){
            name = "%" + name + "%";
            sql += "and name like '"+name + "'";
        }
        if (StringUtils.isNotBlank(status)){

            sql += " and status =" + status;
        }
        sql = sql + " limit ?,?";
        Object [] params = {pageNum,pageSize};
        return baseDao.query(sql,params,RecharGerule.class);
    }

    @Override
    public int findAllAndSearchRecharGeruleCount(String name, String status) {
        String sql = "select count(1) as len from rechargerule where 1 = 1";
        if (StringUtils.isNotBlank(name)){
            name = "%" + name + "%";
            sql += " and name like '"+name + "'";
        }
        if (StringUtils.isNotBlank(status)){

            sql += " and status =" + status;
        }
        List<Map<String, Object>> maps = baseDao.query(sql, null);
        if (maps != null && maps.size() > 0) {
            Map<String, Object> map = maps.get(0);
            Integer res = Integer.parseInt(map.get("len") + "");
            return res;
        }
        return 0;
    }
    @Override
    public List<RecharGerule> getRechargeruleList() {
        String sql = "select * from rechargerule where status=1";
        return baseDao.query(sql,null,RecharGerule.class);
    }
}
